/**
 * 
 */
package com.hanhai.zrb.api.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.hanhai.zrb.api.biz.service.BizMonitorService;
import com.hanhai.zrb.api.biz.service.FundService;
import com.hanhai.zrb.api.biz.service.MessageSendService;
import com.hanhai.zrb.api.biz.service.ProjectService;
import com.hanhai.zrb.api.biz.service.UserDynamicService;
import com.hanhai.zrb.api.biz.service.UserFollowService;
import com.hanhai.zrb.api.biz.service.UserService;
import com.hanhai.zrb.api.biz.service.UserStatisticDetailService;
import com.hanhai.zrb.model.fund.Fund;
import com.hanhai.zrb.model.intention.Intention;
import com.hanhai.zrb.model.intention.enums.IntentionTypeEnum;
import com.hanhai.zrb.model.message.enums.MessageBizType;
import com.hanhai.zrb.model.message.enums.MessageType;
import com.hanhai.zrb.model.project.Project;
import com.hanhai.zrb.model.user.User;
import com.hanhai.zrb.model.user.UserFollow;
import com.hanhai.zrb.model.user.UserStatisticDetail;
import com.hanhai.zrb.model.userDynamic.DynamicType;

/**
 * @author leiruiqi@hanhai.com
 *
 */
@Service("bizMonitorService")
public class BizMonitorServiceImpl implements BizMonitorService{
	
	@Resource
	private MessageBizContentCreateService messageBizContentCreateService;
	
	@Resource
	private MessageSendService messageSendService;
	
	@Resource
	private UserStatisticDetailService userStatisticDetailService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private UserFollowService userFollowService;
	
	@Resource
	private FundService fundService;
	
	@Resource
	private ProjectService projectService;
	
	@Resource
	private UserDynamicService userDynamicService;
	

	/**
	 * 发布项目的监控
	 * 
	 * @param project
	 */
	@Async
	public void projectFirstPublish(Project project){
		String userIdStr = project.getPusher();
		if(StringUtils.isBlank(userIdStr)){
			return;
		}
		Long userId = Long.valueOf(userIdStr);
		User user = userService.findById(userId);
		if(user == null){
			return;
		}
		
		{
			//  入选精选频道的通知
			String content = messageBizContentCreateService.getChoicePublishMsg( project);
			List<Long> receiverUids = new ArrayList<Long>();
			receiverUids.add(userId);
			messageSendService.createMessage(receiverUids, content,MessageType.notify , MessageBizType.choice_publish);
		}
		
		{
			// 被关注人的项目进入精选，发送通知
			String content = messageBizContentCreateService.getAttentionDynamicChoiceMsg(user, project);
			List<Long> receiverUids = userFollowService.followedListAllFromUserId(userId);
			if(CollectionUtils.isNotEmpty(receiverUids)){
				messageSendService.createMessage(receiverUids, content,MessageType.notify , MessageBizType.attention_dynamic_choice);
			}
			
		}
	
		{
			// 发布触发动态事件
			userDynamicService.createUserDynamic(userId, DynamicType.CHOICE_PROJECT, project.getId().toString(), project.getProjectName());
		
		}
	
	}
	
	/**
	 * 发布资金行为的监控
	 * 
	 * @param fund
	 */
	@Async
	public void FundFirstPublish(Fund fund){
		
		Long userId = fund.getUserId();
		User user = userService.findById(userId);
		if(user == null){
			return;
		}
		
		{
			//  入选精选频道的通知
			String content = messageBizContentCreateService.getChoicePublishMsg( fund);
			List<Long> receiverUids = new ArrayList<Long>();
			receiverUids.add(userId);
			messageSendService.createMessage(receiverUids, content,MessageType.notify , MessageBizType.choice_publish);
		}
		
		{
			// 被关注人的项目进入精选，发送通知
			String content = messageBizContentCreateService.getAttentionDynamicChoiceMsg(user, fund);
			List<Long> receiverUids = userFollowService.followedListAllFromUserId(userId);
			if(CollectionUtils.isNotEmpty(receiverUids)){
				messageSendService.createMessage(receiverUids, content,MessageType.notify , MessageBizType.attention_dynamic_choice);
			}
		}
	
		{
			// 发布触发动态事件
			userDynamicService.createUserDynamic(userId, DynamicType.CHOICE_FUND, fund.getId().toString(), fund.getTitlesVal());
		}
	}
	
	/**
	 * 发布意向行为的监控
	 * 
	 * @param intention
	 */
	@Async
	public void intentionPublish(Intention intention){
		Long userId = intention.getUserId();
		User user = userService.findById(userId);
		if(user == null){
			return;
		}
		
		{
			// 被关注人发布意向，发送通知
			String content = messageBizContentCreateService.getAttentionDynamicIntentionMsg(user, intention);
			List<Long> receiverUids = userFollowService.followedListAllFromUserId(userId);
			if(CollectionUtils.isNotEmpty(receiverUids)){
				messageSendService.createMessage(receiverUids, content,MessageType.notify , MessageBizType.attention_dynamic_intention);
			}
		}
	
		{
			// 发布触发动态事件
			DynamicType type = null;
			if(intention.getType() == IntentionTypeEnum.project.getCode()){
				type = DynamicType.PUBLISH_PROJECT;
			}
			else if(intention.getType() == IntentionTypeEnum.fund.getCode()){
				type = DynamicType.PUBLISH_FUND;
			}
			if(type !=null){
				userDynamicService.createUserDynamic(intention.getUserId(), type, intention.getId().toString(), intention.getIntro());
			}

		}
		
	}
	
	/**
	 * 收藏触发的监控
	 * 
	 * @param userStatisticDetail
	 */
	@Async
	public void collection(UserStatisticDetail userStatisticDetail){
		Integer code = userStatisticDetail.getType();
		Long bizId = userStatisticDetail.getDestId();
		UserStatisticDetail.Type type = UserStatisticDetail.Type.getValue(code);
		Long userId = userStatisticDetail.getUserId();
		Long ownerId = null;
		Object model = null;
		if(type ==UserStatisticDetail.Type.fund_col){
			Fund fund = fundService.findById(bizId);
			ownerId = fund.getUserId();
			model = fund;
		}
		
		else if(type ==UserStatisticDetail.Type.pro_col){
			Project project = projectService.selectByPrimaryKey(ownerId);
			ownerId = project.getUserId();
			model = project;
		}
		else{
			throw new RuntimeException("model 类型 不支持");
		}
		
		// user 收藏 owner，给owner发消息，告诉这个owner user收藏了他的xxxx
		User user = userService.findById(userId);
		
		String content = messageBizContentCreateService.getCollectionChoiceMsg(user, model);
		List<Long> receiverUids = new ArrayList<Long>();
		receiverUids.add(ownerId);
		messageSendService.createMessage(receiverUids, content,MessageType.remind , MessageBizType.colleciton_choice);
		
	}
	
	/**
	 * 首次关注触发监控
	 * 
	 * @param userFollow
	 */
	@Async
	public void Fisrtfollow(UserFollow userFollow){
		Long toUserId = userFollow.getToUserId();
		Long fromUserId = userFollow.getFromUserId();
		//User toUser = userService.findById(toUserId);
		User fromUser = userService.findById(fromUserId);
		String content = messageBizContentCreateService.getAttentionUserMsg(fromUser);
		List<Long> receiverUids = new ArrayList<Long>();
		receiverUids.add(toUserId);
		messageSendService.createMessage(receiverUids, content,MessageType.remind , MessageBizType.attention_user);
		
		
	}
	
	
}
